"""
使用aiohttp请求隧道服务器
请求http和https网页均适用

"""
import asyncio
import aiohttp
import requests

# 应用账号（请替换为真实账号）
app_key = 'app_key'
# 应用密码（请替换为真实密码）
app_secret = 'app_secret'
# 用户名密码认证
proxy_auth = aiohttp.BasicAuth(app_key, app_secret)
# 要访问的目标网页
target_url = "http://httpbin.org/ip"
# 隧道域名
proxy_host = 'http-dynamic.xiaoxiangdaili.com'
# 端口号
proxy_port = '10030'


async def fetch(session, url):
    async with session.get(url, proxy="http://" + proxy_host + ":" + proxy_port, proxy_auth=proxy_auth) as response:
        return await response.text()


async def run():
    # aiohttp默认使用严格的HTTPS协议检查。可以通过将ssl设置为False来放松认证检查
    # async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
    async with aiohttp.ClientSession() as session:
        html = await fetch(session, target_url)
        print(html)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
